/**
 *
 */

$(function() {

    $('#save-data').on('click', '.trigger-multi-imgadd', function(e) {
        var _target = $(this).data('target');
        var _elfinderUrl = $(this).data('url');
        var $this = $(this);

        $('<div>').elfinder({
            url : _docroot + 'file/?r=' + _elfinderUrl,
            docked : false,
            closeOnEditorCallback : true,
            dialog : {
                title : 'File Manager',
                height : 500,
                modal : true
            },
            editorCallback : function(img) {
                img = img.replace(/^.+\/upload\/(.+)$/ig, _elfinderReplace);
                $('#' + _target).val(img);
                $('#img_' + _target).prop('src', img).removeClass('ui-helper-hidden');

                $this.removeClass('trigger-imgadd sprite-add').addClass('trigger-imgremove sprite-cross');
            }
        });

        e.preventDefault();
    });

    $('#save-data').on('click', '.trigger-multi-imgremove', function(e) {

        var _target = $(this).data('target');
        var $this = $(this);

        $('#' + _target).val('');
        $('#img-' + _target).prop('src', '').addClass('ui-helper-hidden');

        $this.removeClass('trigger-multi-imgremove sprite-cross').addClass('trigger-multi-imgadd sprite-add');

        e.preventDefault();
    });

    $('#wr-rightcontent select.toggler').chosen().live('change', function(e) {

        var $selected = $(this).find('option:selected');
        var $related = $('#' + $(this).data('fprefix') + '-' + $(this).data('related')).parents('li:first');

        if($selected.data('extra') != undefined) {
            $related.removeClass('ui-helper-hidden-accessible');
        } else {
            $related.addClass('ui-helper-hidden-accessible');
        }

    });

    var myCommands = elFinder.prototype._options.commands;
    var disabled = ['extract', 'archive','netmount'];
    $.each(disabled, function(i, cmd) {
    	(idx = $.inArray(cmd, myCommands)) !== -1 && myCommands.splice(idx,1);
    });


    
    $('#newdataform div.wysiwyg').html('<p></p>').elrte({
        "toolbar" : "eldorado",
        "resizable" : false,
        fmOpen : function(callback) {
        console.log("fmOpen");
        $('<div />').dialogelfinder({
          url: _docroot + 'admin/elfinder/?jx=1',
          commands: myCommands,
          commandsOptions: {
            getfile: {
              oncomplete: 'destroy' 
            }
          },
          getFileCallback:function(file) { callback(file.path); }

        });
        }
    });

    $('#newdataform div.wysiwyg').elrte('val', "<p></p>");

    $('#wr-rightcontent').on('resync', 'div.wysiwyg', function(e) {
        var _related = $(this).data('related');
        $('#' + _related).val($(this).elrte('val'));
    });
    
 
});
